package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.ChannelBean;
import com.project.bean.ThemeBean;
import com.project.bean.UserBean;
import com.project.service.IChannelService;
import com.project.service.IThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;


@RestController
@RequestMapping("theme")
public class ThemeController {
    @Autowired
    private IThemeService themeService;
    @Autowired
    private IChannelService channelService;

    @RequestMapping("cutByItem")
    public IPage<ThemeBean> cutByItem(Integer pageNO, String title, String name, LocalDate startDate, LocalDate endDate, HttpServletRequest req){
        Integer channelId = (Integer) req.getSession().getAttribute("channelId");
        IPage<ThemeBean> ip = themeService.cutByItem(pageNO,channelId,title,name,startDate,endDate);
        return ip;
    }

    @RequestMapping("findById")
    public ChannelBean findById(HttpServletRequest req){
        Integer channelId = (Integer) req.getSession().getAttribute("channelId");
        return channelService.findById(channelId);
    }

    @RequestMapping("add")
    public String add(ThemeBean theme,HttpServletRequest req){
        Integer channelId = (Integer) req.getSession().getAttribute("channelId");
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        theme.setUserId(user.getId());
        theme.setChannelId(channelId);
        themeService.add(theme);
        return "ok";
    }

    @RequestMapping("setThemeId")
    public String setThemeId(Integer themeId,HttpServletRequest req){
        req.getSession().setAttribute("themeId",themeId);
        return "ok";
    }
}
